#include "bits/stdc++.h"

using namespace std;
using i64 = long long;

#define range(a) begin(a), end(a)

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> a(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }

    vector<vector<int>> b(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            b[i][j] = a[j][i];
        }
    }

    auto work = [&](const vector<vector<int>>& v) {
        i64 ans = 0;
        for (int i = 0; i < n; i++) {
            bool ok = 0;
            for (int j = 0; j < n; j++) {
                if (v[i][j] < 0) {
                    ok = 1;
                    break;
                }
            }
            if (ok) {
                for (int j = 0; j < n; j++) {
                    if (v[i][j] < 0) {
                        ans += v[i][j];
                    }
                }
            }
            else {
                ans += *min_element(range(v[i]));
            }
        }
        return ans;
    };

    cout << min(work(a), work(b)) << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}